/* conf.c --
 * Copyright 2009  cwxstat LLC., Elkins Park, Pennsylvania.
 * All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Authors:
 *     Mike Chirico <mchirico@gmail.com>
 *     
 */


/*
This works a bit like C++'s vector.


*/

#include "conf.h"

char *PORT;
char *SERVER;
char *CLIENT;
char *CIPHER_LIST;
char *CAFILE;
char *CADIR;
char *CERTFILE;
char *SERVER_PORT;
char *SERVER_LOGL;
char *CLIENT_LOGL;
char *EXEPROG;
char *USER_WHITE_LIST;
char *HEARTBEAT_SLEEP;
char *HEARTBEAT_LFILE;

/* These will stay local */
static Conf_Buf *build(Conf_Buf * c, char *sec, char *key, char *val);
static void
handle_error(const char *file, int lineno, const char *msg)
{
	syslog(LOG_ERR, "** %s:%i %s\n", file, lineno, msg);
	//ERR_print_errors_fp(stderr);
}

#define int_error(msg)  handle_error(__FILE__, __LINE__, msg)

size_t
StrlenP1(char *s)
{
	if (s == NULL)
		return strlen("(NULL)") + 1;
	return strlen(s) + 1;
}

char *
Strcpy(char *d, char *s)
{
	if (s == NULL)
		return strcpy(d, "(NULL)");
	return strcpy(d, s);

}

Conf_Buf *
get_config_info(const char *Params, const char *Sec_Name)
{
	int i;
	long err = 0;
	char *key;

	STACK_OF(CONF_VALUE) * sec;
	CONF_VALUE *item;
	CONF *conf;

	conf = NCONF_new(NCONF_default());

	Conf_Buf *cb = NULL;

	if (!NCONF_load(conf, CONFFILE, &err)) {
		if (err == 0) {
			int_error("Error opening configuration file");
			return cb;
		}
		else {
			syslog(LOG_ERR, "Error in %s on line %li\n", CONFFILE,
			       err);
			int_error("Errors parsing configuration file");
			return cb;
		}
	}

	if (!(key = NCONF_get_string(conf, Params, Sec_Name))) {
		syslog(LOG_ERR, "Error finding \"%s\" in [%s]\n", Sec_Name,
		       Params);
		int_error("Error finding string");
	}
	//printf("Sec: %s, Key: %s, Val: %s\n", Params, Sec_Name, key);
	if (!(sec = NCONF_get_section(conf, key))) {
		syslog(LOG_ERR, "Error finding [%s]\n", key);
		int_error("Error finding string");
	}
	for (i = 0; i < sk_CONF_VALUE_num(sec); i++) {
		item = sk_CONF_VALUE_value(sec, i);
		/*
		   printf("Sec: %s, Key: %s, Val: %s\n",
		   item->section, item->name, item->value);
		 */
		cb = build(cb, item->section, item->name, item->value);
	}

	// Yeah, everything gets freed
	NCONF_free(conf);

	return cb;
}

Conf_Buf *
build(Conf_Buf * c, char *sec, char *key, char *val)
{

	char *s = NULL;
	char *k = NULL;
	char *v = NULL;
	char **t = NULL;

	s = (char *)malloc(sizeof(char) * StrlenP1(sec));
	if (s == NULL)
		return NULL;
	k = (char *)malloc(sizeof(char) * StrlenP1(key));
	if (k == NULL)
		return NULL;
	v = (char *)malloc(sizeof(char) * StrlenP1(val));
	if (v == NULL)
		return NULL;

	Strcpy(s, sec);
	Strcpy(k, key);
	Strcpy(v, val);

	if (c == NULL) {
		c = (Conf_Buf *) malloc(sizeof(Conf_Buf));
		if (c == NULL)
			return NULL;
		c->sec = NULL;
		c->key = NULL;
		c->val = NULL;
		c->argc = 0;
	}
	c->argc = c->argc + 1;

	t = (char **)realloc(c->sec, sizeof(char *) * c->argc);
	if (t == NULL)
		return NULL;

	t[c->argc - 1] = s;
	c->sec = t;

	t = (char **)realloc(c->key, sizeof(char *) * c->argc);
	if (t == NULL)
		return NULL;

	t[c->argc - 1] = k;
	c->key = t;

	t = (char **)realloc(c->val, sizeof(char *) * c->argc);
	if (t == NULL)
		return NULL;
	t[c->argc - 1] = v;
	c->val = t;

	return c;
}

void
prConf(Conf_Buf * c)
{
	int i;

	if (c == NULL)
		return;
	for (i = 0; i < c->argc; ++i)
		fprintf(stderr, "%s->%s\n", c->key[i], c->val[i]);

	return;
}

Conf_Buf *
freeConf(Conf_Buf * c)
{
	if (c == NULL)
		return NULL;

	int i;
	for (i = 0; i < c->argc; ++i) {
		free(c->sec[i]);
		free(c->key[i]);
		free(c->val[i]);
	}
	free(c->sec);
	free(c->key);
	free(c->val);
	free(c);
	return c;

}

char *
findConf(Conf_Buf * c, const char *s)
{
	int i;
	for (i = 0; i < c->argc; ++i)
		if (strcmp(c->key[i], s) == 0)
			return c->val[i];

	return NULL;
}

/*
int
main(void)
{
	Conf_Buf *c = NULL;
        char *f;

	c =   get_config_info("Params","ServerSection");
	prConf(c);

        f=(char *) malloc(sizeof(char)*80);
        strcpy(f,"server");
	fprintf(stderr, "find(c,%s)=%s\n", f, findConf(c, f));
        strcpy(f,"port");
	fprintf(stderr, "find(c,%s)=%s\n", f, findConf(c, f));
        free(f);
	freeConf(c);

	return 0;
}
*/
